package test.com.newproject.framework.ui;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.ButterKnife;
import test.com.newproject.R;

/**
 * Created by zjh on 2017/8/14.
 */

public abstract class BaseActivity extends AppCompatActivity {
    protected Toolbar toolbar;
    protected TextView titleTextView;

    protected View contentView;
    private boolean resumeFirst = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(R.layout.activity_base);
        ViewGroup contentViewGroup = (ViewGroup) findViewById(R.id.activity_base_content_fl);
        this.toolbar = (Toolbar) findViewById(R.id.activity_base_tb);
        this.titleTextView = (TextView) findViewById(R.id.activity_center_title_tv);

        if (layoutResID != R.layout.activity_base) {
            contentViewGroup.addView(contentView = LayoutInflater.from(this).inflate(layoutResID, null));
        }

        ButterKnife.bind(this);

        if (toolbarVisibility()) {
            initToolbar(obtainToolbar());
        } else {
            toolbar.setVisibility(View.GONE);
        }

        initView();
    }

    /**
     * Toolbar是否显示
     *
     * @return
     */
    protected boolean toolbarVisibility() {
        return true;
    }

    /**
     * 获取Toolbar
     * 可以用自定义Toolbar替代默认
     *
     * @return
     */
    protected Toolbar obtainToolbar() {
        return this.toolbar;
    }

    /**
     * 获取返回键图标
     * 为了图标大小统一建议使用矢量图
     *
     * @return
     */
    protected int obtainHomeIcon() {
        return -1;
    }

    /**
     * 获取Title
     *
     * @return
     */
    protected String obtainTitle() {
        return getClass().getName();
    }

    /**
     * Title是否居中显示
     * 默认居左（Material Design）
     *
     * @return
     */
    protected boolean titleCenter() {
        return false;
    }

    protected void initToolbar(@NonNull Toolbar toolbar) {
        if (this.toolbar != toolbar) {
            toolbar.setVisibility(View.GONE);
        }

        if (obtainHomeIcon() > 0) {
            toolbar.setNavigationIcon(obtainHomeIcon());
        }
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        setTitle(obtainTitle());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //返回键的默认实现是onBackPressed()
            case android.R.id.home:
                onBackPressed();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    protected void initView() {
        // TODO: 2017/8/21 donothing
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (resumeFirst) {
            onResumeFirst();
            resumeFirst = !resumeFirst;
        }
    }

    protected void onResumeFirst() {
        // TODO: 2017/8/21 donothing
    }

    protected void setTitle(String title) {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar == null) {
            return;
        }
        if (titleCenter()) {
            //不显示原生Title
            actionBar.setDisplayShowTitleEnabled(false);
            titleTextView.setText(title);
            titleTextView.setVisibility(View.VISIBLE);
        }

        actionBar.setTitle(title);
    }
}
